package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.Sound;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class SoundApiImpl implements SoundApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public String saveSound(Sound sound) {
        //sound补充完整
        sound.setCreated(System.currentTimeMillis());
        sound.setSid(idWorker.getNextId("sound"));
        //2、调用方法保存对象
        mongoTemplate.save(sound);
        //3、返回对象id
        return sound.getId().toHexString();
    }

    @Override
    public List<Sound> getSound() {
        Query query=new Query();
        return mongoTemplate.find(query,Sound.class);
    }

    @Override
    public void delete(ObjectId id) {
        mongoTemplate.remove(Query.query(Criteria.where("id").is(id)),Sound.class);
    }
}
